Skip to content

gh-162066: Convert LOAD_FAST to LOAD_FAST_BORROW in exception handlers - #162068

Open
eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:load_fast_borrow_handlers
Open

eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:load_fast_borrow_handlers

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

The compiler replaces LOAD_FAST by the cheaper LOAD_FAST_BORROW wherever it can show that the local variable outlives the loaded value. The pass that does this never looks at exception handlers, so code in a handler, or code that can only be reached through one, keeps its LOAD_FAST instructions. This affects except and finally bodies, the cleanup code of with statements, code after a try whose body returns, and all code that follows an async for loop.

This PR makes the pass visit exception handlers as well. The analysis of each block is unchanged.

def f(x, g, h):
    try:
        return g(x)
    except Exception:
        return h(x)     # main: LOAD_FAST h, LOAD_FAST x
                        # PR:   LOAD_FAST_BORROW h, LOAD_FAST_BORROW x

Bytecode statistics for all code objects from Lib/:

main this PR
LOAD_FAST 20,988 9,967
LOAD_FAST_LOAD_FAST 917 133
STORE_FAST_LOAD_FAST 67 48
LOAD_FAST_BORROW 543,213 554,253
LOAD_FAST_BORROW_LOAD_FAST_BORROW 46,371 47,155
strong loads of a local 22,889 10,281
borrowed loads of a local 635,955 648,563
borrowed share 96.5% 98.4%
all instructions 5,184,553 5,184,572

Note that the number of STORE_FAST_LOAD_FAST is very low now, I think the opcode should be removed (see #160921).

Statistics script
"""usage: python lib_stats.py <Lib dir>"""
import collections
import opcode
import os
import sys
import warnings

warnings.simplefilter("ignore")
root = sys.argv[1]


def walk(co):
    yield co
    for c in co.co_consts:
        if hasattr(c, "co_code"):
            yield from walk(c)


def load_kinds(name):
    """Return (#borrowed loads, #strong loads) performed by opcode *name*."""
    if name in ("LOAD_FAST_AND_CLEAR", "LOAD_FAST_CHECK"):
        return 0, 0
    borrowed = name.count("LOAD_FAST_BORROW")
    return borrowed, name.count("LOAD_FAST") - borrowed


files = code_objects = 0
opcodes = collections.Counter()
for dirpath, dirnames, filenames in os.walk(root):
    dirnames[:] = sorted(d for d in dirnames
                         if d not in ("__pycache__", "site-packages"))
    for fn in sorted(filenames):
        if not fn.endswith(".py"):
            continue
        path = os.path.join(dirpath, fn)
        try:
            with open(path, "rb") as f:
                top = compile(f.read(), path, "exec", dont_inherit=True)
        except (SyntaxError, ValueError, RecursionError):
            continue
        files += 1
        for co in walk(top):
            code_objects += 1
            opcodes.update(co.co_code[::2])

named = {opcode.opname[op]: n for op, n in opcodes.items()}
borrowed = strong = 0
for name, n in named.items():
    b, s = load_kinds(name)
    borrowed += b * n
    strong += s * n
print(f"{files} files, {code_objects} code objects")
for name, n in sorted(named.items()):
    if "LOAD_FAST" in name:
        print(f"{name:40} {n:>10,}")
print(f"{'strong loads':40} {strong:>10,}")
print(f"{'borrowed loads':40} {borrowed:>10,}")
print(f"{'all instructions':40} "
      f"{sum(n for name, n in named.items() if name != 'CACHE'):>10,}")

On the default build the effect is one reference count pair per load, so small. On the free-threaded build a strong load of an object can lead to contention.

The benchmark calls one method of a parser object from several threads. The method accepts a size as "4096" or with a suffix as in "100m":

def parse_scaled_int(self, text):
    try:
        return int(text)
    except ValueError:
        pass
    shifts = self.shifts            # dict shared by all threads
    suffix = text[-1]
    if suffix not in shifts:
        raise ValueError(f"invalid size: {text!r}")
    return int(text[:-1]) << shifts[suffix]

The code after except ValueError: pass is only reachable through the handler, so on main it loads shifts with LOAD_FAST. Free-threaded build, time per thread in ms:

threads main PR
1 85.2 84.0 1.01x
2 91.2 89.2 1.02x
4 105.7 103.5 1.02x
8 193.9 172.0 1.13x
16 364.3 250.9 1.45x
Benchmark script
"""Free-threading scaling benchmark for LOAD_FAST_BORROW in exception handlers.

A parser shared by all threads converts sizes given as "4096" or with a
suffix, as in "100m".  It first tries the plain integer form; everything
after the `except ValueError: pass` is only reachable through the exception
handler.
"""
import sys
import threading
import time

CALLS = 20_000


class SizeParser:
    def __init__(self):
        self.shifts = {"k": 10, "m": 20, "g": 30}

    def parse_scaled_int(self, text):
        try:
            return int(text)
        except ValueError:
            pass
        shifts = self.shifts
        suffix = text[-1]
        if suffix not in shifts:
            raise ValueError(f"invalid size: {text!r}")
        return int(text[:-1]) << shifts[suffix]


def worker(parse, barrier, out, idx):
    barrier.wait()
    t0 = time.perf_counter()
    for _ in range(CALLS):
        parse("100m")
        parse("100g")
        parse("21k")
    out[idx] = time.perf_counter() - t0


def run(nthreads):
    parser = SizeParser()   # shared by all threads
    best = float("inf")
    for _ in range(7):
        barrier = threading.Barrier(nthreads)
        out = [0.0] * nthreads
        threads = [threading.Thread(target=worker,
                                    args=(parser.parse_scaled_int, barrier, out, i))
                   for i in range(nthreads)]
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        best = min(best, sum(out) / nthreads)
    return best


if __name__ == "__main__":
    assert not sys._is_gil_enabled()
    print(f"{'threads':>7} {'ms per thread':>14}")
    for nthreads in (1, 2, 4, 8, 16):
        print(f"{nthreads:>7} {run(nthreads) * 1e3:>14.1f}")

@markshannon

Once the SETUP_* pseudo-instructions have been converted, exception
handler blocks are only reachable through the per-instruction exception
edges. optimize_load_fast() only followed jumps and fallthroughs, so the
code in exception handlers was never considered for the LOAD_FAST to
LOAD_FAST_BORROW conversion. Push the exception handler blocks onto the
work list as well.

END_ASYNC_FOR has a jump target that is only used for monitoring, so it
must not be treated as a jump, like in calculate_stackdepth().

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant